package ihistoryApp.service;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ihistoryApp.dal.entity.Answer;
import ihistoryApp.dal.entity.Question;
import ihistoryApp.dal.entity.Story;
import ihistoryApp.dal.mapper.AnswerMapper;
import ihistoryApp.dal.mapper.QuestionMapper;
import ihistoryApp.dal.mapper.StoryMapper;
import ihistoryApp.request.CompleteRequest;
import ihistoryApp.request.GetStoryRequest;
import ihistoryApp.utils.UserUtil;
import ihistoryApp.view.CompleteView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class StoryService {
    @Autowired
    private StoryMapper storyMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private UserUtil userUtil;

    public List<Story> getStoryList(GetStoryRequest request) {
        if (request.getStoryId() == 0) {
            LambdaQueryWrapper<Question> storyLambdaQueryWrapper = new LambdaQueryWrapper<Question>()
                    .select(List.of(Question::getQuestionId))
                    ;
            List<Question> questions = questionMapper.selectList(storyLambdaQueryWrapper);
            questions = RandomUtil.randomEleList(questions, 8);
            return questions.stream().map(question -> {
                LambdaQueryWrapper<Answer> answerLambdaQueryWrapper = new LambdaQueryWrapper<Answer>()
                        .eq(Answer::getQuestionId, question.getQuestionId())
                        ;
                List<Answer> answerList = answerMapper.selectList(answerLambdaQueryWrapper);
                Story story = new Story();
                story.setContent(question.getContent());
                Answer answer = null;
                if (answerList.size() >= 1) {
                    answer = answerList.get(0);
                    story.setSelection1(answer.getAnswerText());
                    if (answer.getIsCorrect() == 1) {
                        story.setAnswer(1);
                    }
                }
                if (answerList.size() >= 2) {
                    answer = answerList.get(1);
                    story.setSelection1(answer.getAnswerText());
                    if (answer.getIsCorrect() == 1) {
                        story.setAnswer(2);
                    }
                }
                if (answerList.size() >= 3) {
                    answer = answerList.get(2);
                    story.setSelection1(answer.getAnswerText());
                    if (answer.getIsCorrect() == 1) {
                        story.setAnswer(3);
                    }
                }
                if (answerList.size() >= 4) {
                    answer = answerList.get(3);
                    story.setSelection1(answer.getAnswerText());
                    if (answer.getIsCorrect() == 1) {
                        story.setAnswer(4);
                    }
                }
                return story;
            }).toList();
        }
        LambdaQueryWrapper<Story> storyLambdaQueryWrapper = new LambdaQueryWrapper<Story>()
                .eq(Story::getStoryId, request.getStoryId())
                ;
        return storyMapper.selectList(storyLambdaQueryWrapper);
    }

    public CompleteView complete(CompleteRequest request) {
        Long score = RandomUtil.randomLong(30L, 60L);
        Long exp = RandomUtil.randomLong(30L, 60L);
        userUtil.addExp(request.getUid(), exp);
        userUtil.addScore(request.getUid(), score);
        CompleteView completeView = new CompleteView();
        completeView.setExp(exp);
        completeView.setScore(score);
        return completeView;
    }

    public List<Story> getSingleModeQuestion() {
        int questionNum = 2;
        List<Long> questionIdList = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .select(List.of(Question::getQuestionId))).stream().map(Question::getQuestionId).toList();
        questionIdList = RandomUtil.randomEleList(questionIdList, questionNum);
        List<Question> questionList = questionMapper.selectBatchIds(questionIdList);
        return questionList.stream().map(question -> {
            List<Answer> answerList = answerMapper.selectList(new LambdaQueryWrapper<Answer>().eq(Answer::getQuestionId, question.getQuestionId()));
            Story story = new Story();
            story.setContent(question.getContent());
            if (answerList.size() >= 1) {
                story.setSelection1(answerList.get(0).getAnswerText());
                if (answerList.get(0).getIsCorrect() == 1) {
                    story.setAnswer(1);
                }
            }
            if (answerList.size() >= 2) {
                story.setSelection2(answerList.get(1).getAnswerText());
                if (answerList.get(1).getIsCorrect() == 1) {
                    story.setAnswer(2);
                }
            }
            if (answerList.size() >= 3) {
                story.setSelection3(answerList.get(2).getAnswerText());
                if (answerList.get(2).getIsCorrect() == 1) {
                    story.setAnswer(3);
                }
            }
            if (answerList.size() >= 4) {
                story.setSelection4(answerList.get(3).getAnswerText());
                if (answerList.get(3).getIsCorrect() == 1) {
                    story.setAnswer(4);
                }
            }
            story.setStoryId(0L);
            story.setQuestionId(question.getQuestionId());
            return story;
        }).toList();
    }
}
